<?php
namespace View;

class ObjectView {

	private $objectID = 'objectid';
	private $userID = 'userid';
	private $catID = 'Catid';
	private $Objtitle = 'Objtitle';
	private $ObjDesc = 'ObjDesc';
	private $Objprice = 'ObjPrice';
	private $submit = 'submit';
	private $edit = 'edit';
	private $editsubmit = 'editsubmit';
	private $delete = 'delete';
	private $editbutton = 'editbutton';

	const OBJECT_EMPTY = 0;
	const OBJECT_SUCCESS = 1;
	const OBJECT_FAILED = 2;
	const OBJECT_SAVED = 3;
	const OBJECT_DELETED = 4;
	const OBJECT_NOT_DELETED = 5;
	//Visar en post när man tryckt på vald OBJECT
	public function showobject(\Model\Object $object, \Model\Database $database) {

		$listLink = NavigationView::getpostListLink();
		$this -> Objtitle = $object -> getTitle();
		$this -> ObjDesc = $object -> getDesc();
		$date = $object -> getDate();
		$this -> objectID = $object -> getId();
		$this -> userID = $object -> getUserId();
		$this -> catID = $object -> getCatId();
		$this -> Objprice = $object -> getPrice();
		$catName = $object -> getCatName();
		
		$loginhandler = new \Model\LoginHandler($database);
		$objecthandler = new \Model\ObjectHandler($database);
		$deletebutton = "";

		//Om man är inloggad så visas deletebutton
		if ($loginhandler -> IsLoggedIn() == true) {
			$deletebutton = $this -> MakeChangesButtons();
		}

		return "
			
				<div class='post'>
					<h2 class='title'>$this->Objtitle</h2> <p>$catName</p>
					<div class='entry'>
					<p>$this->ObjDesc</p>
					</div>
					<div class='byline'>
					<p class='meta'>Auktioneras om: <div id='worldcup_countdown_time'> </div></p>
					<p id='TimeLeft' style='text-indent: -9999px'>$date</p>
					</div>
					$deletebutton
					</br>
					<a href='$listLink'>Tillbaka till listan</a>
				</div>
				
				<div id='comments'></div>";
	}
	
	//Visar lista med poster, när $ListorRoll är true visas hela poster med allt, och när de e false så visas bara länkar med titel
	public function ShowListOfObjects(\Model\ObjectArray $objectArray, $ListorRoll) {
		if ($ListorRoll == true) {
			$ret = "";

			foreach ($objectArray->get() as $object) {
				$linkTopost = NavigationView::getpostLink($object);
				$ret .= "<div class='post'>";
				$ret .= "<h2 class='title'><a href='$linkTopost'>" . $object -> getTitle() . "</a></h2><p> ". $object -> getCatName()."</p>";
				$ret .= "<div class='entry'> <p>" . $object -> getDesc() . "</p></div>";
				$ret .= "<div class='byline'>";
				$ret .= "<p class='meta'>" . $object -> getDate() . "</p>";
				$ret .= "<p class='links'><a href='$linkTopost'>Läs mer</a>&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;<a href='$linkTopost#comments' title='b0x w'>Kommentarer</a></p>";
				$ret .= "</div>";
				$ret .= "</div>";
			}

			return $ret;
		} else {

			$ret = "";
			$i = 0;
			foreach ($objectArray->get() as $object) {
				$linkTopost = NavigationView::getpostLink($object);
				$ret .= "<li><a href='$linkTopost'>" . $object -> getTitle() . "</a></li>";
				if (++$i > 4)
					break;
			}

			return $ret;
		}

	}
	//Visar formulär för nytt inlägg.
	public function NewobjectForm() {

		$xhtml = "
		<h3>Ny annons:</h3>
		<iframe name='upload_iframe' id='upload_iframe' style='display:none;'></iframe> 
			<form action='' method='post' class='post' autocomplete='off'  enctype='multipart/form-data'>
				<p>Titel:</p><input type='text' name='$this->Objtitle' style='width:350px; height:30px; 'required/>
				</br>
				</br>
				<p>Kategori</p>  
				<select name='$this->catID'/>
	  				<option value='1'>Kläder & accessoarer</option>
	  				<option value='2'>Hemelektronik</option>
				</select>
				</br> 
				</br>  
				<p>Beskriving:</p>
				<textarea class='' type='text' name='$this->ObjDesc' style='width:550px; height:120px;' required></textarea>
				</br>
				</br> 
				<p>Utrops pris:</p>
				<input type='text' name='$this->Objprice' style='width:100px; height:30px; ' pattern='[0-9]{2,5}' required/>
				</br>
				</br>
				<p>Tidsgräns för din auktion</p>  
				<select>
  				<option value='1'>3 timmar</option>
  				<option value='2'>6 timmar</option>
  				<option value='3'>12 timmar</option>
				</select>
				</br> 
				</br>
				<div>
				<span>Lägg till en eller flera bilder:</span>
				</br>
				<input type='file' name='picture' id='picture' onchange='return ajaxFileUpload(this);' multiple />
				<span id='picture_error'></span>
				<div id='picture_preview'></div>
				</div>
				
				
				</br> 
				</br> 
				<input type='submit' name='$this->submit' value='Skapa annons' class='delete' style='margin-top:10px;'/>
				
				</form>
				
				
				";
		return $xhtml;
	}
	//Edit formulär
	public function EditobjectForm($post) {
		$title = $post -> GetTitle();
		$content = $post -> GetContent();
		$xhtml = "
		<h3>Redigera inlägg:</h3>
			<form action='' method='post' class='post'> 
				<p>Titel:</p><input type='text' name='$this->Objtitle' value='$title' style='width:350px; height:30px; '/></br>
				<p>Inlägg:</p><textarea class='ckeditor' type='text' name='$this->ObjDesc' style='width:550px; height:320px;' >$content</textarea></br>
				<input type='submit' name='$this->editsubmit' value='Spara' class='delete' style='margin-top:10px;'/>
				</form>";
		return $xhtml;
	}
	//Edit och delete knappar
	public function MakeChangesButtons() {
		$xhtml = "
		<form action='' method='post'> 
			<input type='submit' name='$this->delete' value='Radera' class='delete'/>
			<input type='submit' name='$this->edit' value='Redigera' class='delete'/>
		</form>";
		return $xhtml;
	}

	public function objectID() {
		if (isset($_POST[$this -> objectID]) == true) {
			return $_POST[$this -> objectID];
		}
	}
	public function UserID() {
		if (isset($_POST[$this -> userID]) == true) {
			return $_POST[$this -> userID];
		}
	}
	public function CatID() {
		if (isset($_POST[$this -> catID]) == true) {
			return $_POST[$this -> catID];
		}
	}

	public function Title() {
		if (isset($_POST[$this -> Objtitle]) == true) {
			return $_POST[$this -> Objtitle];
		}
		return null;

	}

	public function Content() {
		if (isset($_POST[$this -> ObjDesc]) == true) {
			return $_POST[$this -> ObjDesc];
		}
		return null;
	}
	
	public function Price() {
		if (isset($_POST[$this -> Objprice]) == true) {
			return $_POST[$this -> Objprice];
		}
		return null;
	}

	public function TriedToSubmit() {
		if (isset($_POST[$this -> submit]) == true) {
			return true;
		}
		return false;
	}

	public function TriedToEditSubmit() {
		if (isset($_POST[$this -> editsubmit]) == true) {
			return true;
		}
		return false;
	}

	public function TriedToDelete() {
		if (isset($_POST[$this -> delete]) == true) {
			return true;
		}
		return false;
	}

	public function TriedToEdit() {
		if (isset($_POST[$this -> edit]) == true) {
			return true;
		}
		return false;
	}

	public function NewObjectMess($status) {
		switch ($status) {
			case self::OBJECT_EMPTY :
				return "<p class='warning'>Titel eller inlägget är tomt!</p>";
				break;
			case self::OBJECT_SUCCESS :
				return "<p class='Success'>Ditt inlägg sparades!</p>";
				break;
			case self::OBJECT_FAILED :
				return "<p class='warning'>Ditt inlägg gick inte att spara!</p>";
				break;
			case self::OBJECT_SAVED :
				return "<p class='Success'>Ditt inlägg sparades!</p>";
				break;
			case self::OBJECT_DELETED :
				return "<p class='warning'>Ditt inlägg raderades!</p>";
				break;
			case self::OBJECT_NOT_DELETED :
				return "<p class='warning'>Ditt inlägg raderades inte!</p>";
				break;
			default;
			break;
		}
	}

}
